/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.cpu.z80;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.In;
import com.dustedpixels.cirt.model.core.Out;
import com.dustedpixels.cirt.model.core.Pins;
import com.dustedpixels.cirt.model.core.Script;

/**
 * Alu
 *
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class Alu {
  @In @Pins(8) public int dataIn1;
  @In @Pins(8) public int dataIn2;
  @In @Pins(4) public int op;
  @In @Pins(8) public int flagsIn;
  @Out @Pins(8) public int dataOut;
  @Out @Pins(8) public int flagsOut;
  
  @Script
  public void update() {
    switch (op) {
      case 0x00:
        // ADD
        dataOut = (dataIn1 + dataIn2) & 0xFF;
        break;
      case 0x01:
        // ADC
        dataOut = (dataIn1 + dataIn2 + (flagsIn & 0x01)) & 0xFF;
        break;
      case 0x02:
        // SUB
        dataOut = (dataIn1 - dataIn2) & 0xFF;
        break;
      case 0x03:
        // SBC
        dataOut = (dataIn1 - dataIn2 - (flagsIn & 0x01)) & 0xFF;
        break;
      case 0x04:
        // AND
        dataOut = dataIn1 & dataIn2;
        break;
      case 0x05:
        // OR
        dataOut = dataIn1 | dataIn2;
        break;
      case 0x06:
        // XOR
        dataOut = dataIn1 ^ dataIn2;
        break;
      case 0x07:
        // CP
        break;
        
      // TODO(micapolos): Shift operations.
    }
  }
}
